﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;


namespace IntroduceCompany.Util
{
    public class GeneralClass
    {
        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to dd/mm/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1(String strDate)
        {
            return DateTime.Parse(strDate).ToString("dd/MM/yyyy").ToString();
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm:ss)
        /// </summary>
        /// <param name="strDate">Datetime</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1B(DateTime strDate)
        {
            if (strDate != null)
            {
                return strDate.ToString("dd/MM/yyyy HH:mm:ss").ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm  AM/PM)
        /// </summary>
        /// <param name="strDate">Datetime</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1C(String strDate)
        {
            if (strDate != "")
            {
                return DateTime.Parse(strDate).ToString("dd/MM/yyyy HH:mm tt").ToString();
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// Convert date (dd/MM/yyyy) or (dd/MM/yyyy hh:mm:ss GMT) to MM/dd/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String MM/dd/yyyy</returns>
        public static String ConvertDate_2(String strDate)
        {

            if (strDate != null && strDate != "")
            {
                string[] splDate = strDate.Split(new char[] { '/', ' ' });
                strDate = splDate[1] + "/" + splDate[0] + "/" + splDate[2];
            }
            return strDate;
        }


        /// <summary>
        /// Convert Vietnamese string to Engish string
        /// </summary>
        /// <param name="str"> Vietnamese string</param>
        /// <returns>Englisth string</returns>
        public static string filter(string str)
        {
            string[] VietNamChar = new string[] 
            { 
                "aAeEoOuUiIdDyY", 
                "áàạảãâấầậẩẫăắằặẳẵ", 
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
                "éèẹẻẽêếềệểễ", 
                "ÉÈẸẺẼÊẾỀỆỂỄ", 
                "óòọỏõôốồộổỗơớờợởỡ", 
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
                "úùụủũưứừựửữ", 
                "ÚÙỤỦŨƯỨỪỰỬỮ", 
                "íìịỉĩ", 
                "ÍÌỊỈĨ", 
                "đ", 
                "Đ", 
                "ýỳỵỷỹ", 
                "ÝỲỴỶỸ"
            };
            //Thay thế và lọc dấu từng char      
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                    str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
            }
            return str;
        }


        /// <summary>
        /// Replace Html Special Character (&,',<,>)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ReplaceSpecialCharacters(string input)
        {
            input = input.Replace("&", "&amp;");
            input = input.Replace("'", "&apos;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt;");
            return input;
        }

        /// <summary>
        /// cut string in description
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static String CutString(String str, int length)
        {
            Boolean flag = true;
            while (flag)
            {
                try
                {
                    string temp = str.Substring(str.IndexOf('<'), str.IndexOf('>') - str.IndexOf('<') + 1);
                    str = str.Replace(temp, "");
                    if (temp == "")
                        flag = false;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            str = str.Length > length ? str.Substring(0, length).Substring(0, str.Substring(0, length).LastIndexOf(' ')) + " ..." : str;
            return str.Replace("  ", " ");
        }


        /// <summary>
        /// Encode MD5
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetMD5Hash(string value)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}